import {
	getActivity,
	getActivityHomeData,
	getActivityItemDetailPage,
	getItemAppointmentByGaApid
} from "../../utils/api";

let state = {
	activity: {},
	activityItems: [],
	activityStores: [],
	activityBanners: [],
	currentActivityItem: {},
	currentActivityItemStores: [],
	currentActivityItemBanners: [],
	currentActivityItemSpellOrders: [],
	currentActivityItemGraphicDetails: [],
	appointment: [],
};

let getters = {
	activity: (state) => state.activity,
	activityStores: (state) => state.activityStores,
	activityItems: (state) => state.activityItems,
	activityBanners: (state) => state.activityBanners,
	currentActivityItem: (state) => state.currentActivityItem,
	currentActivityItemStores: (state) => state.currentActivityItemStores,
	currentActivityItemBanners: (state) => state.currentActivityItemBanners,
	currentActivityItemSpellOrders: (state) => state.currentActivityItemSpellOrders,
	currentActivityItemGraphicDetails: (state) => state.currentActivityItemGraphicDetails,
	appointment: (state) => state.appointment,
};

let mutations = {};

let actions = {
	async loadActivity({
		state
	}, payload) {
		state.activity = await getActivity();
		if (state.activity?.gpUuid) {
			let data = await getActivityHomeData({
				gpUuid: state.activity.gpUuid,
				bannerName: "activity_carousel_picture"
			});
			state.activityStores = data.activityStores || [];
			state.activityItems = data.activityItems || [];
			if (new Date().getUTCTime() >= state.activity.gpStti) {
				state.activityBanners = data.activityBanners?.filter(item => item.scPlay === "1" || item
					.scPlay === "3") || [];
			} else {
				state.activityBanners = data.activityBanners?.filter(item => item.scPlay === "1" || item
					.scPlay === "2") || [];
			}

		}
	},

	async getItemAppointment({
		state
	}, payload) {
		state.appointment = await getItemAppointmentByGaApid({
			gpUuid: payload.gpUuid,
			gaApid: payload.gaApid,
		});
	},
    
    async getActivityByStaff({
    	state
    }, payload) {
    	state.activity = await getActivity();
    },

	async loadCurrentActivityItem({
		state
	}, payload) {
		const {
			giUuid,
			gpUuid
		} = payload;
		if (state.currentActivityItem.giUuid && giUuid === state.currentActivityItem.giUuid) return;
		const data = await getActivityItemDetailPage({
			giUuid,
			gpUuid,
			bannerName: "activity_item_carousel_picture",
			graphDetailName: "activity_item_detail_picture",
		});
		state.currentActivityItem = data.activityItem || {};
		state.currentActivityItemStores = data.stores || []
		if (new Date().getUTCTime() >= state.activity.gpStti) {
			state.currentActivityItemBanners = data.activityItemsBanners.filter(item => item.scPlay === "1" ||
				item.scPlay === "3") || [];
			state.currentActivityItemGraphicDetails = data.activityItemGraphicDetails.filter(item => item
				.scPlay === "1" || item.scPlay === "3") || [];
		} else {
			state.currentActivityItemBanners = data.activityItemsBanners.filter(item => item.scPlay === "1" ||
				item.scPlay === "2") || [];
			state.currentActivityItemGraphicDetails = data.activityItemGraphicDetails.filter(item => item
				.scPlay === "1" || item.scPlay === "2") || [];
		}
		state.currentActivityItemSpellOrders = data.activityItem.groups?.nodes || [];
	},
};

export default {
	namespaced: true,
	state,
	getters,
	mutations,
	actions,
};
